//
//  WmcWatermarkCell.swift
//  YPWatermarkCamera
//
//  Created by 赵新 on 2022/3/4.
//

import Kingfisher
import UIKit
import YPBedrock

public extension WmcButtonItemType {
    enum SelectWMItem {
        /// 分享
        case share

        /// 编辑
        case edit

        /// 自定义
        case custom(view: UIView)
    }
}

// MARK: - [WmcWatermarkCell]

class WmcWatermarkCell: WmcBaseCollectionCell {
    lazy var editBtn: UIButton = {
        let btn = UIButton.Wmc_themeColorBtn(color: .wmc_mainTheme, title: "编辑")
        btn.titleLabel?.font = 14.yp.uiFont(weight: .bold)
        btn.yp.layoutWidthEqual(60)
        return btn
    }()

    lazy var shareBtn: UIButton = {
        let btn = UIButton.Wmc_whiteColorBtn(color: .wmc_mainTheme, title: "分享")
        btn.titleLabel?.font = 14.yp.uiFont(weight: .bold)
        btn.yp.layoutWidthEqual(60)
        return btn
    }()

    lazy var starkView: UIStackView = {
        let starkView = UIStackView()
        starkView.alignment = .fill
        starkView.axis = .horizontal
        starkView.distribution = .fill
        starkView.spacing = 6
        return starkView
    }()

    lazy var selectMaskView: UIView = {
        let view = UIView()
        view.backgroundColor = .black.yp.alpha(0.7)
        view.layer.cornerRadius = 5
        view.layer.masksToBounds = true
        return view
    }()

    var shareBlock: YPBlock.OneParam<WmcWatermarkDetailModel>?
    var editBlock: YPBlock.OneParam<WmcWatermarkDetailModel>?

    var selectWatermarkDelegate: WmcSelectWatermarkDelegate? {
        return YPWmcManager.shared.uiDelegate.selectWatermarkDelegate
    }

    override func makeUI() {
        contentView.layer.cornerRadius = 5
        contentView.layer.masksToBounds = true
        contentView.layer.borderColor = UIColor.wmc_mainTheme.cgColor

        addSubview(imageView)
        imageView.snp.makeConstraints { make in
            make.top.left.equalTo(6)
            make.right.equalTo(-6)
            make.bottom.equalTo(-40)
        }

        imageView.addSubview(selectMaskView)
        selectMaskView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }

        titleLabel.textColor = .wmc_grayH2_323233
        titleLabel.font = 14.wmc_font
        titleLabel.textAlignment = .center
        addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.left.right.bottom.equalToSuperview()
            make.top.equalTo(imageView.snp.bottom)
        }

        addSubview(starkView)
        starkView.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.centerY.equalTo(imageView)
        }
    }

    func configModel(_ model: WmcWatermarkDetailModel, isSelect: Bool) {
        imageView.kf.setImage(with: model.templateImg.yp.url, placeholder: "wm_loading".wmc_image)
        titleLabel.text = model.name

        if isSelect {
            selectMaskView.isHidden = false
            starkView.isHidden = false
            contentView.layer.borderWidth = 2
            reloadItems(watermarkModel: model)
        } else {
            selectMaskView.isHidden = true
            starkView.isHidden = true
            contentView.layer.borderWidth = 0
        }

        editBtn.yp.addEvent { [weak self] _ in
            guard let weakSelf = self else { return }
            weakSelf.editBlock?(model)
        }

        shareBtn.yp.addEvent { [weak self] _ in
            guard let weakSelf = self else { return }
            weakSelf.shareBlock?(model)
        }
    }

    func reloadItems(watermarkModel: WmcWatermarkDetailModel) {
        var defaultList: [WmcButtonItemType.SelectWMItem] = []
        if YPWmcManager.shared.isIndependentPage {
            defaultList = [
                .share, .edit,
            ]
        } else {
            defaultList = [
                .edit,
            ]
        }
        if let _defaultList = selectWatermarkDelegate?.watermarkSelectItemList(.current,
                                                                               watermarkModel: watermarkModel,
                                                                               defaultList: defaultList)
        {
            defaultList = _defaultList
        }
        let views = toItemList(defaultList)
        starkView.yp.arrangedSubviews = views
    }

    func toItemList(_ caseList: [WmcButtonItemType.SelectWMItem]) -> [UIView] {
        var views: [UIView] = []
        caseList.forEach { cas in
            switch cas {
            case .edit:
                views.append(editBtn)
            case .share:
                views.append(shareBtn)
            case let .custom(view: view):
                views.append(view)
            }
        }
        return views
    }
}
